/*****************************************************************************
 *
 *                      HUSONG PROPRIETARY INFORMATION
 *
 *          The information contained herein is proprietary to HUSONG
 *           and shall not be reproduced or disclosed in whole or in part
 *                    or used for any design or manufacture
 *              without direct written authorization from HUSONG.
 *
 *            Copyright (c) 2015 by HUSONG.  All rights reserved.
 *
 *****************************************************************************/
package me.send.produce.client.controller;

import java.util.List;

import me.send.business.service.bean.BrokerEvaluate;
import me.send.business.service.bean.ReturnJsonBean;
import me.send.business.service.bean.UserEvaluateBean;
import me.send.business.service.cons.OrderCons;
import me.send.business.service.cons.ProgressCons;
import me.send.business.service.pojo.OrderProgress;
import me.send.business.service.service.BrokerEvaluateService;
import me.send.business.service.service.OrderProgressService;
import me.send.business.service.service.OrdersService;
import me.send.business.service.service.UserEvaluateService;
import me.send.framework.util.HelperUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author maxmin
 *
 * @date 2015年8月25日 下午5:01:43
 */
@Controller
@RequestMapping(value = "/evaluate")
public class EvaluateController {
	@Autowired
	UserEvaluateService userEvaluateService;
	@Autowired
	BrokerEvaluateService brokerEvaluateService;
	@Autowired
	OrdersService ordersService;
	
	@Autowired
	OrderProgressService orderProgressService;

	/**
	 * 添加对经纪人的评价
	 * 
	 * @param evaluate
	 * @return
	 * @author maxmin
	 * @date 2015年8月25日下午5:16:33
	 */
	@RequestMapping()
	@ResponseBody
	@Transactional
	public ReturnJsonBean evaluate(BrokerEvaluate evaluate, String devicenumber) {
		ReturnJsonBean rb = new ReturnJsonBean();
		int result = brokerEvaluateService.addEvaluate(evaluate);
		final int orderid = evaluate.getOrderid();
		int ret = ordersService.completEvaluate(orderid);
		if (result > 0 && ret>0) {
			new Thread(new Runnable() {
				public void run() {
					//保存进度
					OrderProgress op= new OrderProgress();
					op.setUserMsg(ProgressCons.ORDER_EVALUATED_MSG);
					op.setContent(ProgressCons.ORDER_EVALUATED_CONTENT);
					op.setOrderid(orderid);
					op.setOrderStatus(OrderCons.ORDER_FINISHED);
					op.setCreatetime(HelperUtil.getCurrentDate());
					op.setUpdatetime(HelperUtil.getCurrentDate());
					orderProgressService.addOrderProgress(op);
					OrderProgress op1= new OrderProgress();
					op1.setUserMsg("");
					op1.setContent(ProgressCons.ORDER_FINISHED);
					op1.setOrderid(orderid);
					op1.setOrderStatus(OrderCons.ORDER_FINISHED);
					op1.setCreatetime(HelperUtil.getCurrentDate());
					op1.setUpdatetime(HelperUtil.getCurrentDate());
					orderProgressService.addOrderProgress(op1);
				}
			}).start();
			rb.setCode(ReturnJsonBean.SUCCESS);
			rb.setMessage(ReturnJsonBean.SUCCESS_STR);
		}
		return rb;
	}

	/**
	 * 跳转到对经纪人评价页面
	 * @param devicenumber
	 * @param userid
	 * @param brokerid
	 * @return
	 * @author maxmin
	 * @date 2015年9月2日上午10:33:49
	 */
	@RequestMapping(value="toEvaluate")
	@ResponseBody
	public ReturnJsonBean toEvaluate(String devicenumber, int userid,
			int orderid) {
		ReturnJsonBean rb = new ReturnJsonBean();
		try {
			rb.setDatas(brokerEvaluateService.toEvaluate(userid, orderid));
			rb.setCode(ReturnJsonBean.SUCCESS);
			rb.setMessage(ReturnJsonBean.SUCCESS_STR);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rb;
	}
	
	/**
	 * 我对经纪人的评价列表
	 * @param brokerid
	 * @param type 1:我对经纪人的评价 2：经纪人对我的评价
	 * @param devicenumber
	 * @return
	 * @author maxmin
	 * @date 2015年9月16日下午5:26:31
	 */
	@RequestMapping(value="myevaluates")
	@ResponseBody
	public ReturnJsonBean myEvaluates(int userid,Long startid,int size,int type,String devicenumber){
		ReturnJsonBean rb = new ReturnJsonBean();
		try {
			if (type==1) {
				rb.setCode(ReturnJsonBean.SUCCESS);
				rb.setMessage(ReturnJsonBean.SUCCESS_STR);
				List<UserEvaluateBean> list = brokerEvaluateService.myEvaluates(userid, startid, size);
				rb.setDatas(list);
			}else if (type==2) {
				rb.setCode(ReturnJsonBean.SUCCESS);
				rb.setMessage(ReturnJsonBean.SUCCESS_STR);
				List<UserEvaluateBean> list = userEvaluateService.evaluateMe(userid, startid, size);
				rb.setDatas(list);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rb;
	}
	
}
